package cate.game.role.farm.forbidden;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDForbidden;
import cate.common.table.d.GDMail;
import cate.common.table.farm.forbidden.row.ForbiddenCombatRow;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.farm.forbidden.msg.ForbiddenAutoStatusResp;
import cate.game.role.farm.forbidden.msg.ForbiddenChallengeResultResp;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.ArrayList;
import java.util.List;

public class ForbiddenAutoStatus extends RoleDependent {

    @NoteField(value = "是否自动挑战中")
    public boolean autoing;

    @NoteField(value = "神殿类型")
    public int roomId;

    @NoteField(value = "当前挑战层数")
    public int currentFloor;

    @NoteField(value = "通关层数")
    public int passFloor;

    @NoteField(value = "剧本开始时间")
    public long playStartTime;

    @NoteField(value = "剧本开始时间")
    public long playEndTime;

    @NoteField(value = "累积奖励")
    public MixRes reward;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (reward == null) {
            reward = new MixRes();
        }
    }

    private void clear() {
        autoing = false;
        roomId = -1;
        currentFloor = 0;
        passFloor = 0;
        playStartTime = 0L;
        playEndTime = 0L;
        reward = new MixRes();
    }

    public void handleResult(boolean win, MixRes reward) {
        if (reward != null && !reward.isEmpty()) {
            this.reward.addList(reward.copy());
        }
        if (win) {
            this.passFloor++;
            if (passFloor < GDForbidden.MAX_FLOOR) {
                // 尝试挑战下一关
                this.noticeUpdate();
                ForbiddenCombatRow combatTpl = role.getGame().table.forbidden.combat.find(roomId, passFloor);
                EcResult<Void> r = role.getFarm().forbidden.challenge(roomId, passFloor, combatTpl, true);
                if (!r.ok()) {
                    onStop();
                }
            } else {
                onStop();
            }
        } else {
            onStop();
        }
    }

    public void onChallenge(int roomId, int floor, long playStartTime, long playEndTime) {
        if (!autoing) {
            this.autoing = true;
            //跨天切换副本
            if (this.roomId != roomId) {
                onStop();
            }
        }
        this.currentFloor = floor;
        this.playStartTime = playStartTime;

        // 根据播放速度篡改结束时间
        long passTime = (long) ((playEndTime - playStartTime) / GDFight.FAST_SPEED_RATE);
        this.playEndTime = playStartTime + passTime;
    }

    /**
     * 挑战结束时
     */
    public void onStop() {
        // 发奖励
        if (!reward.isEmpty()) {
            if (role.getStatus().isOnline()) {
                this.reward.add(role, true, 0);
            } else {
                List<Object> params = new ArrayList<>();
                params.add(GDForbidden.Type.toFuncId(roomId));
                params.add(passFloor);
                Mail mail = new MailBuilder()
                        .setReward(this.reward.copy())
                        .setContentId(GDMail.Tid.FORBIDDEN_RANK_REWARD)
                        .setParamList(params)
                        .build();
                role.getGame().mail.send(role.getUid(), mail);
            }
            role.send(new ForbiddenChallengeResultResp(GDForbidden.Type.toFuncId(roomId), true, this.passFloor, this.reward));
        } else {
            role.send(new ForbiddenChallengeResultResp(GDForbidden.Type.toFuncId(roomId), false, this.passFloor, this.reward));
        }

        clear();
        noticeUpdate();
    }

    public void noticeUpdate() {
        role.sendNow(new ForbiddenAutoStatusResp(this));
    }
}
